//
//  FirstCollectView.m
//  
//
//  Created by 郭明皓 on 15/10/23.
//
//

#import "FirstCollectView.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "TuijianCell.h"
#import "GMHActivity.h"
#import "GMHDataFiled.h"
#import "GMHFindDetailViewController.h"
#import "GMH_Share.h"
#import "DKNightVersion.h"
@interface FirstCollectView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain )UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *tuijianArr;
@property (nonatomic, assign)int pageindex;
@property (nonatomic, assign)BOOL isUploading;
@end

@implementation FirstCollectView
- (void)dealloc
{
    [_tuijianArr release];
    [_tableView release];
    [super dealloc];
}
//- (void)viewDidAppear:(BOOL)animated
//{
//    if ([GMH_Share SingleShare].isNight == YES) {
//        self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
//    }
//    [self.tableView reloadData];
//    [self.tableView reloadData];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"门票";
    
    self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 90) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    
    self.tableView.nightBackgroundColor = [UIColor blackColor];
    self.tableView.nightSeparatorColor = [UIColor darkGrayColor];
    self.tuijianArr = [NSMutableArray array];
    [self downPull];
    [self upPull];
}
//下拉刷新
- (void)downPull
{
    __block FirstCollectView *first = self;
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageindex = 1;
        self.isUploading = NO;
        [first create];
    }];
    [self.tableView.header beginRefreshing];
    
}
- (void)upPull
{
    __block FirstCollectView *first = self;
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.tableView.footer endRefreshing];
        self.pageindex++;
        self.isUploading = YES;//标记为上拉操作
        [first create];
    }];
}
- (void)create
{
    [GMHDataFiled isHaveFileDocument];
    [GMHDataFiled isHaveFileActivity];
    //collectview点击数据
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        NSInteger number = [self.tag integerValue];
               NSString *str = [NSString stringWithFormat:@"http://appapi.yaochufa.com/v2/Product/GetProductList?machineCode=ffffffff-9317-02f2-ffff-fffff60c2478&tagId=%ld&city=%%E5%%A4%%A7%%E8%%BF%%9E&latitude=38.882871&channel=xiaomimarket&pageSize=20&themeId=&sort=n&type=tag&version=4.3.1&system=android&pageIndex=%d&propertyId=0&longitude=121.544181", number, _pageindex];
    if (_isUploading == NO) {
        [self.tuijianArr removeAllObjects];
    }
        [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = responseObject;
           NSMutableArray* arr = [GMHSurroundModel baseModelArry:dic[@"data"][@"items"]];
            [self.tuijianArr addObjectsFromArray:arr];
            if (_tuijianArr.count > 0 ) {
                [self.tableView.header endRefreshing];
                [self.tableView.footer endRefreshing];
                [self.tableView reloadData];
            }
            
            NSString *sendBoxPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
            NSString *CachesPath = [sendBoxPath stringByAppendingPathComponent:@"Travel/Activity/collectdetail.plist"];
            [NSKeyedArchiver archiveRootObject:dic toFile:CachesPath];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"门票失败");
        }];
   
    }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.tuijianArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strs = @"HometableView";
    TuijianCell *cell = [tableView dequeueReusableCellWithIdentifier:strs];
    if (!cell) {
        cell = [[TuijianCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strs];
    }
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//去掉分割线
    GMHSurroundModel *tuijian = self.tuijianArr[indexPath.row];
    [cell.leftimageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://cdn5.jinxidao.com/%@", tuijian.mImageUrl]]placeholderImage:[UIImage imageNamed:@"travel.jpg"]];
    cell.title.text = tuijian.productName;
    cell.describe.text = tuijian.productTitleContent;
    cell.money.text = [NSString stringWithFormat:@"￥%@",tuijian.price];
    cell.normalMoney.text = [NSString stringWithFormat:@"￥%@", tuijian.retailPrice];
    cell.sell.text = [NSString stringWithFormat:@"已售%@", tuijian.saledCount];
    // 字体删除线
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", tuijian.retailPrice]];
    NSRange contentRange = {0.9,[content length]};
    [content addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleThick] range:contentRange];
    cell.normalMoney.attributedText = content;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GMHFindDetailViewController *detail = [[GMHFindDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
    GMHSurroundModel *tuijian = self.tuijianArr[indexPath.row];
    detail.productIds = tuijian.productId;
    detail.model = tuijian;
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
